<?php

class controller extends controllerBase {
	
	public function index()
	{
		$title = 'blog';
		$content = 'this is the main page for blogs';
	}
	
	public function add()
	{
		$title = 'blog :: add';
		
		global $headers;
		
		$errors = array();
	
		if(isset($_POST['submit']))
		{
			#process form
			
			$newBlog = new blog();
			
			#check title
			if($_POST['title'] == '')
			{
				$errors[] = 'no-title';
			}
			else
			{
				$newBlog->title = $_POST['title'];
			}
			
			#check content
			if($_POST['content'] == '')
			{
				$errors[] = 'no-content';
			}
			else
			{
				$newBlog->content = $_POST['content'];
			}
			
			if($_POST['url'] == '')
			{
				$errors[] = 'no-url';
			}
			else
			{
				$newBlog->url = $_POST['url'];
			}
			
			if(module::isActive('tag'))
			{
				#check tags
				if($_POST['tags'] == '')
				{
					$errors[] = 'no-tags';
				}
				else
				{
					$tags = explode(';', $_POST['tags']);
					foreach ($tags as $t)
					{
						$t = strtolower(trim($t));
					}
				}
			}
			#future post?
			if($_POST['future'] == 'future')
			{
				if($_POST['month'] == '' || $_POST['day'] == '' || $_POST['year'] == '' || $_POST['hour'] == '' || $_POST['min'] == '')
				{
					$errors[] ='invalid-date';
				}
				else
				{
					$newBlog->publishDate = mktime($_POST['hour'], $_POST['min'], 0, $_POST['month'], $_POST['day'], $_POST['year']);
				}
			}
			else
			{
				$newBlog->publishDate = time();
			}
			
			
			$newBlog->date = time();
			
			
			if(count($errors) <= 0)
			{
				$newBlog->save();
				
				if(module::isActive('tag'))
				{
					#insert tag relations
					for($i = 0; $i < count($tags); $i++)
					{
						
						$query = new Query("SELECT * FROM `tags` WHERE `tag` = ? ORDER BY `id`", $tags[$i]);
						
						if($query->num_rows() > 0)
						{
							$p = $query->fetch_assoc();
							
							$tag = new tag($p['id']);
						}
						else
						{
							$tag = new tag();
							$tag->tag = $tags[$i];
							$tag->save();
						}
						
						
						$query = new Query("INSERT INTO `blog_tag_relations` (`tag`, `blog`) VALUES (?, ?)", $tag->id, $newBlog->id);
					}
				}
				unset($_POST);
				$submitted = true;
			}
			
			if(module::isActive('updates'))
			{
				$update = new updates();
				$update->blog = $newBlog->id;
				$update->date = $newBlog->publishDate;
				$update->save();
			}
			
		}
		
		if($submitted)
		{
			#redirect
			$content =  "blog entry added";
		}
		else
		{
			#show form
			$content = form::getForm('blog_add', array($errors));
			
		}
		
		include(ROOT . 'admin/skin/wrapper.php');
	}
	
	public function delete($id = '')
	{
		if($id != '')
		{
			$blog = new blog_blog($id);
			new Query("DELETE FROM `blog_tag_relations` WHERE `blog` = ?", $id);
			new Query("DELETE FROM `updates` WHERE `blog` = ?", $id);
			
			$blog->delete();
			$content = 'blog deleted';
		}
		include(ROOT . 'admin/skin/wrapper.php');
	}
	
	public function edit($id = '')
	{
		global $headers;
		$title = 'edit blogs';
		
		if($id == '')	//no page set, show all
		{
			$query = new Query("SELECT * FROM `blogs` ORDER BY `date` DESC");
			$blog = blog::RecordSet($query, 'blog');
			
			$content .= '<h2>edit blog entries</h2>';
			
			
			for($i = 0; $i < count($blog); $i++)
			{
				$content .=   '<p>' . $blog[$i]->title . ' : <a href="' . ROOT_URL .'admin/' . URL_BASE . 'blog/edit/' . $blog[$i]->id . '">Edit</a> - <a href="' . ROOT_URL . 'admin/' . URL_BASE . 'blog/delete/' . $blog[$i]->id . '">Delete</a></p>';
			}
		}
		else	//get that specific page for editing
		{
		
			$errors = array();
			
			$blog = new blog($id);
			
			$title .= ' :: ' . stripslashes($blog->title);
			
			if(isset($_POST['submit']))
			{
				#process form
				
				
				
				#check title
				if($_POST['title'] == '')
				{
					$errors[] = 'no-title';
				}
				else
				{
					$blog->title = $_POST['title'];
				}
				
				#check content
				if($_POST['content'] == '')
				{
					$errors[] = 'no-content';
				}
				else
				{
					$blog->content = $_POST['content'];
				}
				
				if($_POST['url'] == '')
				{
					$errors[] = 'no-url';
				}
				else
				{
					$blog->url = $_POST['url'];
				}
				
				
				//$blog->date = time();
				
				
				if(count($errors) <= 0)
				{
					$blog->save();
					


					
					unset($_POST);
					$submitted = true;
				}
			}
			
			if($submitted)
			{
				#redirect
				$content =  "blog entry updated";
			}
			else
			{
				#show form
				$content =  form::getForm('blog_edit', array($errors, $blog));
				
			}
		}
		
		include(ROOT . 'admin/skin/wrapper.php');
	}
}
?>